<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="40vw"
    append-to-body
       :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="元素名称" prop="elementTitle">
        <el-input
          v-model="form.elementTitle"
          placeholder="请输入"
          :maxlength="20"
        ></el-input>
      </el-form-item>
      <el-form-item label="元素来源" prop="elementFrom">
        <el-select v-model="form.elementFrom" placeholder="请选择元素来源">
          <el-option :key="1" label="平台" :value="1"></el-option>
          <el-option :key="2" label="供应商" :value="2"></el-option>
          <el-option :key="3" label="算力用户" :value="3"></el-option>
          <el-option :key="4" label="中国算力网" :value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="元素类型" prop="elementType">
        <el-select v-model="form.elementType" placeholder="请选择元素类型">
          <el-option :key="1" label="数据集" :value="1"></el-option>
          <el-option :key="2" label="模型" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="元素共享类型" prop="elementShareType">
        <el-select
          v-model="form.elementShareType"
          placeholder="请选择元素共享类型"
        >
          <el-option :key="0" label="私密" :value="0"></el-option>
          <el-option :key="1" label="共享" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否可下载" prop="downloadType">
        <el-select v-model="form.downloadType" placeholder="请选择是否可下载">
          <el-option :key="-1" label="不可以" :value="-1"></el-option>
          <el-option :key="0" label="可以" :value="0"></el-option>
          <el-option :key="1" label="线下协商" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="元素框架"
        prop="elementFrame"
        v-if="form.elementType === 2"
      >
        <el-select v-model="form.elementFrame" placeholder="请选择元素框架">
          <el-option
            v-for="dict in dict.type.element_frame"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="平台推荐" prop="recommendStatus">
        <el-select v-model="form.recommendStatus" placeholder="请选择">
          <el-option :key="0" label="否" :value="0"></el-option>
          <el-option :key="1" label="是" :value="1"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="研究方向、研究领域" prop="elementDomainList">
        <el-select
          v-model="form.elementDomainList"
          multiple
          placeholder="请选择研究方向、研究领域"
        >
          <el-option
            v-for="dict in dict.type.element_domain"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="元素标签" prop="elementTagList">
        <el-select
          v-model="form.elementTagList"
          multiple
          placeholder="请选择元素标签"
        >
          <el-option
            v-for="dict in dict.type.element_tag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="描述">
        <editor v-model="form.remark" :min-height="192" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  addElementProvide,
  updateElementProvide,
} from "@/api/element/elementProvide";
export default {
  name: "ElementPopup",
  dicts: ["element_domain", "element_frame", "element_tag"],
  props: {
    visible: Boolean,
    formData: [Object, null],
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      loading: false,
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
    title() {
      if (this.formData && this.formData.id) return "修改元素信息";
      return "添加元素信息";
    },
  },
  watch: {
    dialogVisible(val) {
      if (val && this.formData) {
        this.form = structuredClone(this.formData);
      }
    },
  },
  methods: {
    handleClose() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        remark: null,
        elementFrom: null,
        elementType: null,
        elementShareType: null,
        downloadTime: null,
        status: null,
        downloadType: null,
        elementFrame: null,
        downloadTimes: null,
        collectTimes: null,
        // recommendStatus: null,
        elementDomainList: [],
        elementTagList: [],
        updateUserId: null,
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.form.id != null) {
            updateElementProvide(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
              })
              .finally(() => {
                this.loading = false;
                this.dialogVisible = false;
              });
          } else {
            addElementProvide(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
              })
              .finally(() => {
                this.loading = false;
                this.dialogVisible = false;
              });
          }
        }
      });
    },
  },
};
</script>
